// packageB/serviceOrderDetail/index.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
import moment from 'moment';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: {
            1: "未派单",
            2: "已派单",
            3: "已完成",
            4: "取消预约",
            5: "已退款",
        },
        obj:'',
        resourceURL: wx.api.host + '/',
    },
    //添加评论
  toCommon(event) {
 
    let {send_id,order_id}=this.data.obj
    wx.navigateTo({
      url: '/packageB/comments/oneComment/index?send_id=' + send_id+'&order_id='+order_id,
    })
  },
    getList() {
        Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 0,
        })
        wx.http.get(wx.api.orderserviceinfo, {
            send_id: this.data.send_id,
        }).then((res) => {
            if (res.code == 1) {
                res.data[0].make_time = this.formatCreateTime(new Date(res.data[0].make_time).getTime())
                res.data[0].pay_time = this.formatCreateTime(res.data[0].pay_time*1000)
                res.data[0].top_pic = this.data.resourceURL+res.data[0].top_pic
                this.setData({
                    obj: res.data[0]
                })
 
                setTimeout(() => {
                    Toast.clear()
                }, 500);
            }
        })
    },
    //取消预订
    cancel(event) {
        let that = this
        Dialog.confirm({
            confirmButtonColor: "#ff7733",
            cancelButtonColor: "rgba(32,35,39,0.5)",
            message: "确认取消预订吗？",
          })
          .then(() => {
            that.cancelCollect();
    
          })
          .catch(() => {
    
          });
      },
 //转换时间格式
 formatCreateTime(val) {
    return moment(val).calendar(null, {
      sameDay: "MM月DD日 [今天] HH:mm:ss",
      nextDay: "MM月DD日 [明天] HH:mm:ss",
      nextWeek: "MM月DD日  HH:mm:ss",
      lastDay: "MM月DD日 [昨天] HH:mm:ss",
      lastWeek: "MM月DD日  HH:mm:ss",
      sameElse: 'YYYY年MM月DD日  HH:mm:ss'
    });
  },
  cancelCollect() {

    wx.http.post(wx.api.serviceafter, {
      send_id:this.data.obj.send_id
    }).then(res => {
      if (res.code == 1) {
        wx.navigateBack()
        Toast.success(res.msg);
      } else {
        Toast.fail(res.msg);
      }

    })


  },
    /**
     * 生命周期函数--监听页面加载
     */

    onLoad(options) {
    
        this.setData({
            send_id:options.send_id
        })
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})